import { _decorator, Component, Event, EventTouch, Node, Tween, tween, UITransform, v3, Vec2 } from 'cc';
import { WorldCar } from './WorldCar';
import { Panel, UIManager } from '../../Scripts/Framework/Managers/UIManager';
import { GameData } from './GameData';
import { ChangeManager } from './ChangeManager';
import { ProjectEventManager, ProjectEvent } from '../../Scripts/Framework/Managers/ProjectEventManager';
const { ccclass, property } = _decorator;

@ccclass('WorldManager')
export class WorldManager extends Component {

    private static instance: WorldManager;
    public static get Instance(): WorldManager {
        return this.instance;
    }

    @property(Node)
    world: Node;

    @property(Node)
    tutorial: Node;

    isMove = false;
    target: Node;

    protected onLoad(): void {
        WorldManager.instance = this;
        ProjectEventManager.emit(ProjectEvent.弹出窗口);
    }

    start() {
        this.tutorial.active = GameData.Instance.getNumberData("Tutorial") == 0;
    }

    update(deltaTime: number) {

    }

    TouchStart(event: EventTouch) {
        this.isMove = false;
        for (let i = 0; i < this.world.children.length; i++) {
            const element = this.world.children[i];
            if (element.getComponent(WorldCar)) continue;
            if (element.getComponent(UITransform).getBoundingBoxToWorld().contains(event.getUILocation())) {
                this.target = element;
                Tween.stopAllByTarget(this.target);
                tween(this.target)
                    .to(0.5, { scale: v3(1, 0.5, 1) })
                    .start();
                break;
            }
        }
    }

    TouchMove(event: EventTouch) {
        this.isMove = true;
        let delta = event.getDelta();
        let pos = this.world.getPosition();
        pos.x += delta.x;
        this.world.setPosition(pos);
        if (this.target && !this.target.getComponent(UITransform).getBoundingBoxToWorld().contains(event.getUILocation())) {
            Tween.stopAllByTarget(this.target);
            tween(this.target)
                .to(0.5, { scale: v3(1, 0.5, 1) })
                .start();
            this.target = null;
        }
    }

    TouchEnd(event: EventTouch) {
        if (this.isMove) return;
        for (let i = 0; i < this.world.children.length; i++) {
            const element = this.world.children[i];
            if (element.getComponent(WorldCar)) continue;
            if (element.getComponent(UITransform).getBoundingBoxToWorld().contains(event.getUILocation())) {
                this.PlayGame(element.name);
                break;
            }
        }
        if (this.target && !this.target.getComponent(UITransform).getBoundingBoxToWorld().contains(event.getUILocation())) {
            Tween.stopAllByTarget(this.target);
            tween(this.target)
                .to(0.5, { scale: v3(1, 0.5, 1) })
                .start();
            this.target = null;
        }
    }

    Play(event: Event) {
        if (this.tutorial.active && event.target.name != "Game0") return;
        this.PlayGame(event.target.name);
    }

    PlayGame(name: string) {
        GameData.gameName = name;
        UIManager.ShowPanel(Panel.LoadingPanel, "GameScene");
    }

    ChangeCharacter() {
        ChangeManager.GoChangeScene();
    }

}


